//
//  XWScanResultViewController.swift
//  XWSurSpeed
//
//  Created by hopemobi-1 on 2024/8/8.
//

import UIKit
import WebKit

class XWScanResultViewController: UIViewController {
    var XWResultString:String!
    
    @IBOutlet weak var XWWebView: WKWebView!
    @IBOutlet weak var XWMessageBackView: UIView!
    
    @IBOutlet weak var XWMessageLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        showUI()
    }
    @IBAction func XWBackButtonClick(_ sender: UIButton) {
        dismiss(animated: true, completion: nil)
    }
    
}
extension XWScanResultViewController{
    private func showUI(){
        let isUrlString = isValidWebURL(XWResultString)
        if isUrlString {
            XWMessageBackView.isHidden = true
            XWWebView.isHidden = false
            XWWebView.load(URLRequest(url: URL(string: XWResultString)!))
        }else{
            XWMessageBackView.isHidden = false
            XWWebView.isHidden = true
            XWMessageLabel.text = XWResultString
        }
    }
    private func isValidWebURL(_ urlString: String) -> Bool {
        let urlRegEx = "^(https?://)?(www\\.)?[a-zA-Z0-9_-]*(\\.[a-zA-Z0-9_-]*)+([/?].*)?$"
        let regex = try! NSRegularExpression(pattern: urlRegEx)
        let results = regex.matches(in: urlString, range: NSRange(urlString.startIndex..., in: urlString))
        return results.count > 0
    }
}


